<template>
	<div v-show="visible">
		<van-field readonly clickable :label="componentModel.label" v-model="vue_model_label" @click="showPicker()" />
		<van-popup v-model:show="showPicker_model" position="bottom">
			<van-picker show-toolbar :columns="selectLabelList" @cancel="closePicker()" @confirm="onConfirm" />
		</van-popup>
	</div>
</template>

<script>
	import {
		sendComponentValueChangeRequest
	} from '@/utils/requestUtils.js';
	import utils from '@/utils/utils.js';
	export default {
		name: "SelectComponent",
		props: ["componentModel", "scope"],
		data() {
			return {
				vue_model: "",
				vue_model_label: "",
				showPicker_model: false,
				selectlist: [],
				selectLabelList: [],
				visible: true,
				readonly: false,
				label: "",
			}
		},
		inject:['children'],
		mounted() {
			this.children.push(this);
		},beforeUnmount() {
			utils.removeArrayElement(this.children,this);
		},
		methods: {
			onConfirm(value) {
				this.vue_model_label = value;
				this.showPicker_model = false;
				this.onValueChangeHandler();
			},
			getValue() {
				for (var i = 0; i < this.selectlist.length; i++) {
					if (this.selectlist[i].name == this.vue_model_label) {
						return this.selectlist[i].value;
					}
				}
			},
			onValueChangeHandler() {
				if (this.scope != undefined) {
					this.$emit("onValueChange", this);
					return;
				}
				sendComponentValueChangeRequest(this);
			},
			setProp(prop) {
					var dataModel = prop["options"];
					if (dataModel != undefined) {
						this.selectlist = dataModel;
						this.selectLabelList.length = 0;
						for (var i = 0; i < this.selectlist.length; i++) {
							this.selectLabelList.push(this.selectlist[i].name);
						}
					}
					for (var k in prop) {
						if (k == "value") {
							if (prop[k] == null) {
								this.vue_model = "";
							} else {
								this.vue_model = prop[k];
								for (i = 0; i < this.selectlist.length; i++) {
									if (this.selectlist[i].value == this.vue_model) {
										this.vue_model_label = this.selectlist[i].name;
										break;
									}
								}
							}
						} else if (k == "visible") {
							this.visible = prop[k];
						} else if (k == "readonly") {
							this.readonly = prop[k];
						} else if (k == "label") {
							this.label = prop[k];
						}
					}
			},
			showPicker() {
				if (this.readonly) {
					return;
				}
				this.showPicker_model = true;
			},
			closePicker() {
				this.showPicker_model = false
			}
		}
	}
</script>

<style>
</style>
